import axios, {AxiosInstance} from "axios"
import {ElMessage} from "element-plus"


const user:any = localStorage.getItem("token")
const json = JSON.parse(user);


let instance :AxiosInstance ;

if(json == null || json.token == ""){
    instance = axios.create({
        baseURL: '/api',
        timeout: 10000
    });
}else{
    instance =  axios.create({
        baseURL: '/api',
        timeout: 10000,
        headers: { 'Authorization': json.token },
    });
}


// 添加请求拦截器
instance.interceptors.request.use(function (config:any) {
    // 在发送请求之前做些什么
    return config;
}, function (error:any) {
    // 对请求错误做些什么
    ElMessage({
        message: "网络出现了，错误请稍后再试！",
        type: 'error',
        plain: true,
    })
    return Promise.reject(error);
});

// 添加响应拦截器
instance.interceptors.response.use(function (response:any) {
    // 2xx 范围内的状态码都会触发该函数。
    // 对响应数据做点什么
  const data = response.data.data
    console.log("后端响应数据",response)
    switch (data.status) {
        case 200:
            ElMessage({
                message: data.message,
                type: 'success',
                plain: true,
            })
            break;
        case 400:
            ElMessage({
                message: data.message,
                type: 'error',
                plain: true,
            })
            break;
        case 404:
            ElMessage({
                message: data.message,
                type: 'error',
                plain: true,
            })
            break;
        case 500:
            ElMessage({
                message: data.message,
                type: 'error',
                plain: true,
            })
            break;
        default:{
            ElMessage({
                message: "服务端异常！",
                type: 'error',
                plain: true,
            })
            break;
        }
    }
    return response;
}, function (error:any) {
    // 超出 2xx 范围的状态码都会触发该函数。
    // 对响应错误做点什么
    try{
        if (error.response.status === 401){
            ElMessage({
                message: "你的登录已经失效，请重新登录！",
                type: 'error',
                plain: true,
            })
            setTimeout(()=>{
                localStorage.clear()
                window.location.reload()
            },1500)
        }else if(error.response.status === 403){
            ElMessage({
                message: "你没有权限访问此页面，请联系管理员！",
                type: 'error',
                plain: true,
            })
        } else {
            ElMessage({
                message: "网络出现了错误,请稍后再试！",
                type: 'error',
                plain: true,
            })
        }
        return Promise.resolve(error);
    }catch (e){
        ElMessage({
            message: "网络出现了错误,请稍后再试！",
            type: 'error',
            plain: true,
        })
        return Promise.resolve(error);
    }

});

const get = (url: string, data: object) => {
    return new Promise((reslove, reject) => {
        instance.get(url, data).then((result:any) => {
            reslove(result.data.data)
        }).catch((e) => {
            reject(e)
        })
    })
}

const post = (url:string,data:object) =>{
    return new Promise((reslove, reject) => {
        instance.post(url, data).then((result:any) => {
            reslove(result.data.data)
        }).catch((e) => {
            reject(e)
        })
    })
}

const put = (url:string,data:object) =>{
    return new Promise((reslove, reject) => {
        instance.put(url, data).then((result:any) => {
            reslove(result.data.data)
        }).catch((e) => {
            reject(e)
        })
    })
}

const del = (url:string,data:object) =>{
    return new Promise((reslove, reject) => {
        instance.delete(url, data).then((result:any) => {
            reslove(result.data.data)
        }).catch((e) => {
            reject(e)
        })
    })
}

const login = (url:string,data:object) =>{
    return new Promise((reslove, reject) => {
        instance.post(url, data).then((result:any) => {
            reslove(result.data.data)
        }).catch((e) => {
            reject(e)
        })
    })
}

export {get,post,put,del,login};
